/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ages.test.swat

import com.ages.entity.Actor
import com.ages.env.Options
import com.ages.graphics.GLDraw
import com.ages.graphics.Sprite
import com.ages.graphics.ViewportSwitcher
import com.ages.input.Keyboard
import com.ages.input.Mouse
import com.ages.math.Vector3D
import com.sun.opengl.util.GLUT
import javax.media.opengl.GL
import scala.collection.mutable.ArrayBuffer

object Swatter{
  val sprite = new Sprite("/com/ages/test/res/silverswat.png",1,1,1)
}

class Swatter(p:Vector3D,mouse:Mouse,keys:Keyboard) extends Actor{
  pos = p
  depth = DepthPlaces.FRONT
  var charge = 0.0
  var zdist = 0.0
  val SPEED = 0.03
  val MOUSE_SPEED = 15
  var spawnListeners = new ArrayBuffer[SpawnListener]()
  var timeout = 0
  val IDLE = 0
  val CHARGING = 1
  val SMACKING = 2
  var strikingPower:Double = 0
  var state = IDLE

  def addSpawnListener(sl:SpawnListener)
  {
    spawnListeners += sl
  }
  def think(){
    var x = 0.5-(mouse.getX-Options.windowX)/Options.width.toDouble
    var y = (mouse.getY-Options.windowY)/Options.height.toDouble-0.5
    pos = Vector3D(-x*MOUSE_SPEED,y*(5-MOUSE_SPEED),zdist)
    timeout-=1
    if(keys.space && timeout<=0)
    {
      timeout=60
      for(s<-spawnListeners)
      {
//           s.newWorldObject(new Smack(Vector3D(mouse.getX,mouse.getY,1)))
//           println("new objects added!..")
      }
    }
    if(mouse.leftDown && (state==IDLE || state==CHARGING))
    {
      state = CHARGING
      charge+=0.7
      if(charge>60)
        charge=60
    }
    else if(charge>0 && (state==CHARGING || state==SMACKING))
    {
      if(state!=SMACKING)
        {
          strikingPower=charge
      state=SMACKING
        }
      charge-=14
      if(charge<=0)
      {
        charge=0
        state=IDLE
        for(s<-spawnListeners)
        {
          s.newWorldObject(new Smack(Vector3D(mouse.getX-Options.windowX,110+Options.height-(mouse.getY-Options.windowY),1),strikingPower))
        }
        strikingPower=0
      }
    }
//        println("done thinking...")
//    println(pos)
  }

  def draw(gl:GL)
  {

    def ball = () =>{
      val mat_ambient = Array[Float](1,0,0,1)
      val mat_diffuse = Array[Float](1f,0.0f,0.0f,1)
      val mat_specular = Array[Float](0,0,0,1)
      val high_shininess = Array[Float](10.0f)
      val no_mat = Array[Float](0,0,0,1)
      gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, mat_ambient,0);
      gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_diffuse,0);
      gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular,0);
      gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, high_shininess,0);
      gl.glMaterialfv(GL.GL_FRONT, GL.GL_EMISSION, no_mat,0);
      gl.glColor4d(1,1,1,1)
      gl.glMatrixMode(GL.GL_MODELVIEW)
      val glut = new GLUT()
//      gl.glTranslated(0,-3,0)//make it rotate around the bottom shaft
      gl.glRotated(-90+charge/4,1,0,0)
//      gl.glTranslated(0,3,0)

      gl.glTranslated(0,2,0)//try to rotate around shaft

      gl.glEnable(GL.GL_TEXTURE_2D)
//      Swatter.sprite.spriteBuffer.spriteSheet.bind
      val SIZE = 8

      Swatter.sprite.bind

      gl.glBegin(GL.GL_QUADS)
      gl.glTexCoord2f(0,1)
      gl.glVertex3d(0,0,0)
      gl.glTexCoord2f(0,0)
      gl.glVertex3d(0,0,SIZE)
      gl.glTexCoord2f(1,0)
      gl.glVertex3d(SIZE,0,SIZE)
      gl.glTexCoord2f(1,1)
      gl.glVertex3d(SIZE,0,0)
      gl.glEnd
//      gl.glDisable(GL.GL_TEXTURE_2D)


//      gl.glPushMatrix
//      gl.glScaled(0.4,0.2,3)
////      gl.glColor4d(1,1,1,0.3)
//      glut.glutSolidCube(1)
//      gl.glPopMatrix
//
//      gl.glPushMatrix
//      gl.glTranslated(0,0,2)
//      gl.glScaled(2,0.2,2)

//      glut.glutSolidCube(1)
//      gl.glPopMatrix

    }

    val spot:Vector3D = Vector3D(pos.X,pos.Y,pos.Z-7)
    ViewportSwitcher.setup3DView(gl)
    gl.glDisable(GL.GL_LIGHTING)

    gl.glPushMatrix
    gl.glTranslated(-1.2,-4.4,0)

    GLDraw draw ball at spot on gl
    gl.glPopMatrix
  }

  override def collidesWith(other:Actor):Boolean ={
    var res = false
//    if(other.isInstanceOf[Platform])
//      {
//        val diff = pos minus other.getPos
//        if(diff.Y<1 && diff.Y>0.3 && Math.abs(diff.X)<2.35 && vel.Y<0 )
//          {
////            println("true for "+this)
//            res = true
//          }
//      }
    res
  }

  override def handleCollision(other:Actor)
  {
//            val opos = other.getPos
//            pos = Vector3D(pos.X,opos.Y+1,opos.Z)
//            vel = Vector3D(vel.X,0,vel.Z)
//            println("handling collision with "+other)
  }

}
